﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.SessionState;
using Newtonsoft;
using Newtonsoft.Json;
using System.Web.Script.Services;
using DB.DataService;

/// <summary>
/// Summary description for AdminService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class AdminService : System.Web.Services.WebService
{

    private static ProductsService productservice;
    public AdminService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
        productservice = new ProductsService();
    }
    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void Login()
    {
        string result = "";
        /* Dung phuong thuc Get
            string username = System.Web.HttpContext.Current.Request.QueryString["User"];
            string password = System.Web.HttpContext.Current.Request.QueryString["Pass"];
         */
        /* Dùng phuong thuc POST */
        string username = System.Web.HttpContext.Current.Request.Form.Get("User");
        string password = System.Web.HttpContext.Current.Request.Form.Get("Pass");
        bool b = productservice.CheckUserNamePassword(username, password);
        if (b)
        {
            Utilities.Utilities utilites = new Utilities.Utilities();
            //Khai bao cookie de luu tru username va password
            System.Web.HttpContext.Current.Response.Cookies["thaithinh"]["username"] = Utilities.Utilities.Encrypt(username, "thaithinh",true);
            System.Web.HttpContext.Current.Response.Cookies["thaithinh"]["password"] = Utilities.Utilities.Encrypt(password, "thaithinh", true); 
            System.Web.HttpContext.Current.Response.Cookies["thaithinh"].Expires = DateTime.Now.AddDays(1);

            //System.Web.HttpContext.Current.Response.Cookies["logout"].Value = Utilities.Utilities.Encrypt(username, "thaithinh", true);
            //System.Web.HttpContext.Current.Response.Cookies["logout"].Expires = DateTime.Now.AddMinutes(360);
            //Khoi tao 1 cookie. truong hop nay k bi tao instant giong truong hop tao cookie o tren. Truong hop o tren la moi lan goi thi tao 1 instant moi vi k co khoi tao
            //System.Web.HttpCookie cookie = new HttpCookie("logout");
            //cookie.Name = "logout";
            //cookie.Value = username;
            //cookie.Expires = DateTime.Now.AddDays(1);
            //HttpContext.Current.Response.Cookies.Add(cookie);
            result = "{result:true}";

        }
        else
        {
            result = "{result:false}";
        }
        HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.DeserializeObject(result));
    }
    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void LogOut()
    {
        try
        {

            string result = "{result:true}";
           // System.Web.HttpContext.Current.Response.Cookies["Login"].Expires = DateTime.Now;
            //System.Web.HttpContext.Current.Response.Cookies["logout"].Expires = DateTime.Now.AddMinutes(-360);
            HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.DeserializeObject(result));

        }
        catch (Exception)
        {

        }

    }

}
